//
//  AudioPlayPageViewController.m
//  QDaily_Program
//
//  Created by dllo on 15/12/10.
//  Copyright © 2015年 WXCLegendary. All rights reserved.
//

#import "AudioPlayPageViewController.h"

@interface AudioPlayPageViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, AudioPlayerDelegate>
@property (nonatomic, retain)NSMutableArray *audioArray;
@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)MBProgressHUD *HUD;
@property (nonatomic, assign)NSInteger loadNum;
@end

@implementation AudioPlayPageViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createCollectionView];
    _HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    _HUD.color = [UIColor blackColor];
    _HUD.dimBackground = YES;
    _HUD.labelText = @"正在加载";
    [self getAudioPageNetData];
//    [self loadMoreData];
    _loadNum = 1;
    
  self.navigationItem.title = @"视频";
    
   
    
//    [self addheader];
    
}


- (void)changeCollectionView {
//    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//    NSString *password = [user objectForKey:@"night"];
//    if ([password isEqualToString:@"yes"]) {
//        self.collectionView.backgroundColor = [UIColor grayColor];
//    }else {
//        self.collectionView.backgroundColor = [UIColor colorWithRed:214 / 255.0 green:213 / 255.0 blue:183 / 255.0 alpha:1];
//    }
//    [self.collectionView reloadData];
}

//协议方法
- (void)passAudioPlayerModel:(AudioPlayerModel *)audioPlayerModel {
    AudioPlayerViewController *audioVC = [[AudioPlayerViewController alloc] init];

    audioVC.audioPlayerModel = audioPlayerModel;
    [self presentViewController:audioVC animated:YES completion:^{
    
    }];
}



- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 10;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) collectionViewLayout:layout];
    [_collectionView registerClass:[AudioPlayPageCollectionViewCell class] forCellWithReuseIdentifier:@"AudioPlayPageCollectionViewCellIdentifier"];
    self.collectionView.backgroundColor = [UIColor grayColor];
    _collectionView.delegate = self;
    
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.audioArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AudioPlayPageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AudioPlayPageCollectionViewCellIdentifier" forIndexPath:indexPath];
    cell.delegate = self;
    if (indexPath.item < self.audioArray.count) {
        cell.audioPlayerModel = self.audioArray[indexPath.item];
       
        
    }
    return cell;
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(self.view.width, self.view.width);
}

//网络请求
- (void)getAudioPageNetData {
    [WXCNetWorkTool getWithUrl:kAudioURL parameter:nil httpHeader:nil responseType:ResponseTypeJson success:^(id result) {
        self.audioArray = [[NSMutableArray alloc] init];
        NSArray *videoList = [result objectForKey:@"videoList"];
        for (NSDictionary *dic in videoList) {
            AudioPlayerModel *audioPlayModel = [[AudioPlayerModel alloc] initWithDictionary:dic];            
            [self.audioArray addObject:audioPlayModel];

        }
        [self.collectionView reloadData];
        [_HUD hide:YES afterDelay:1];
    } fail:^(NSError *error) {
        [_HUD hide:YES];
    }];
}

////上拉加载
//
//- (void)loadMoreData {
//    [self.collectionView addFooterWithCallback:^{
//            NSString *string = [NSString stringWithFormat:@"http://c.3g.163.com/nc/video/home/%ld0-10.html", ++_loadNum];
//            //继续网络请求
//            [WXCNetWorkTool getWithUrl:string parameter:nil httpHeader:nil responseType:ResponseTypeJson success:^(id result) {
//                NSArray *videoList = [result objectForKey:@"videoList"];
//                for (NSDictionary *dic in videoList) {
//                    AudioPlayerModel *audioPlayModel = [[AudioPlayerModel alloc] initWithDictionary:dic];
//                    [self.audioArray addObject:audioPlayModel];
//                    [audioPlayModel release];
//            }
//                [self.collectionView reloadData];
//                [self.collectionView footerEndRefreshing];
//            } fail:^(NSError *error) {
//            }];
//        
//    }];
//}

#pragma mark --- 下拉刷新

//- (void)addheader {
//    [self.collectionView addHeaderWithCallback:^{
//        [WXCNetWorkTool getWithUrl:kAudioURL parameter:nil httpHeader:nil responseType:ResponseTypeJson success:^(id result) {
//            self.audioArray = [[NSMutableArray alloc] init];
//            NSArray *videoList = [result objectForKey:@"videoList"];
//            for (NSDictionary *dic in videoList) {
//                AudioPlayerModel *audioPlayModel = [[AudioPlayerModel alloc] initWithDictionary:dic];
//                [_audioArray addObject:audioPlayModel];
//                [audioPlayModel release];
//            }
//            [_collectionView reloadData];
//        } fail:^(NSError *error) {
//        }];
//        [self.collectionView headerEndRefreshing];
//    }];
//}
//










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
